﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VivahMuhurth.DBAccess;
using System.Drawing;
using System.Drawing.Imaging;


public partial class SignUpProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDownList();
            lblStatus.Visible = false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (true)// do all validations.
        {
            int UserFreeTrialMonths = ConfigurationManager.AppSettings["UserFreeTrialMonths"] != null ? Convert.ToInt32(ConfigurationManager.AppSettings["UserFreeTrialMonths"].ToString()) : Constants.DefaultFreeTrialMonths;
            DBAccess dBAccess = new DBAccess();

            bool response = dBAccess.InsertLoginMaster(txtFirstName.Text, txtLastName.Text, txtEmail.Text, txtPassword.Text, false, true, DateTime.Now.AddMonths(UserFreeTrialMonths));

            if (response)
            {
                lblStatus.Text = response ? Resources.Resource.SignUpSuccess : Resources.Resource.SignUpFailure;

                string fullName = txtFirstName.Text + txtLastName.Text;
                string emailMsg = Utility.getWelcomTextOnSignup(fullName);
                //Utility.sendEmail(txtEmail.Text, emailMsg);
                Utility.sendEmail("sanb313@gmail.com", emailMsg);
            }
            else
            {
                lblStatus.Text = "Email address is already available,Please use another email-Id.";
                lblStatus.Visible = true; 
            }
            clearControls();
        }
    }
    private void clearControls()
    {
        txtFirstName.Text = txtLastName.Text = txtConfirmEmail.Text = txtEmail.Text = txtConfirmPassword.Text = txtPassword.Text = lblStatus.Text = string.Empty;
    }

    private void BindDropDownList()
    {
        try
        {
            DBAccess dBAccess = new DBAccess();

            ddlReligion.DataSource = dBAccess.GetReligion(); ddlReligion.DataBind();
            ddlCast.DataSource = dBAccess.GetCast(); ddlCast.DataBind();
            ddlSubcast.DataSource = dBAccess.GetSubCast(); ddlSubcast.DataBind();
            ddlOccupation.DataSource = ddlFatherOccupatioin.DataSource = ddlMotherOccupation.DataSource= dBAccess.GetOccupation();
            ddlOccupation.DataBind(); ddlFatherOccupatioin.DataBind(); ddlMotherOccupation.DataBind();
            ddlEducation.DataSource = dBAccess.GetEducation(); ddlEducation.DataBind();
            ddlHeight.DataSource = dBAccess.GetHeightlist(); ddlHeight.DataBind();
            ddlWeight.DataSource = dBAccess.GetWeightlist(); ddlWeight.DataBind();
            ddlMotherTongue.DataSource = dBAccess.GetLanguagelist(); ddlMotherTongue.DataBind();
        }
        catch
        {
            throw;
        }
    }
    protected void ddlReligionChanged(object sender, EventArgs e)
    {
        string ReligionId = ddlReligion.SelectedValue;

        DataTable dtTemp = ((DataTable)(ddlCast.DataSource)).Clone();
        DataRow[] CastRows = ((DataTable)(ddlCast.DataSource)).Select("ReligionId = " + ReligionId);

        foreach (DataRow dr in CastRows)
            dtTemp.ImportRow(dr);
        ddlCast.DataSource = dtTemp; ddlCast.DataBind();

        if (dtTemp != null && dtTemp.Rows.Count > 0)
        {
            DataTable dtTempCast = ((DataTable)(ddlSubcast.DataSource)).Clone();
            DataRow[] SubCastRows = ((DataTable)(ddlSubcast.DataSource)).Select("ReligionId = " + ReligionId);

            foreach (DataRow dr in SubCastRows)
                dtTempCast.ImportRow(dr);
            ddlSubcast.DataSource = dtTempCast; ddlSubcast.DataBind();
        }

    }
    protected void ddlCastChanged(object sender, EventArgs e)
    {
        string CastId = ddlCast.SelectedValue;

        DataTable dtTemp = ((DataTable)(ddlSubcast.DataSource)).Clone();
        DataRow[] CastRows = ((DataTable)(ddlSubcast.DataSource)).Select("CastId = " + CastId);

        foreach (DataRow dr in CastRows)
            dtTemp.ImportRow(dr);
        ddlSubcast.DataSource = dtTemp; ddlSubcast.DataBind();
    }

    #region " Clear fields "
    private void clearBasicInfo()
    {
        ddlProfile.SelectedIndex = rblGender.SelectedIndex = rblMaritialStatus.SelectedIndex = rblHaveChilden.SelectedIndex = 0;
        // datepicker
        txtPartnerPreference.Text = txtAboutMe.Text = string.Empty;
    }
    private void clearReligiousInfo()
    {
        ddlReligion.SelectedIndex = ddlCast.SelectedIndex = ddlSubcast.SelectedIndex = 0;
        txtGana.Text = txtGotra.Text = txtNakshtra.Text = txtBirthTime.Text = string.Empty;
        rblMangalik.SelectedIndex = 0;
        ddlSunSign.SelectedIndex = 0;
    }
    private void clearCarrierInfo()
    {
        ddlEducation.SelectedIndex = ddlOccupation.SelectedIndex = ddlResidentialType.SelectedIndex = 0;
        txtAnnualIncome.Text = string.Empty;
    }
    private void clearPhysicalInfo()
    {
        ddlHeight.SelectedIndex = ddlWeight.SelectedIndex = 0;
        rblBloodGroup.SelectedIndex = rblBodyType.SelectedIndex = rblComplexion.SelectedIndex = rblDietType.SelectedIndex = 0;
        chkPhysicallyChallenged.Checked = chkIsSmoke.Checked = chkIsDrink.Checked = chkIsSpects.Checked = false;
    }
    private void clearFamilyBackgroud()
    {
        txtFatherName.Text = txtMotherName.Text = txtNumberofBrothers.Text = txtNumberOfSisters.Text = txtMarriedBros.Text = txtMarriedSisters.Text = txtKnownRelatives.Text = txtAboutMyfamily.Text = string.Empty;
        chkIsJointFamily.Checked = false;
        rblFamilyValues.SelectedIndex = 0;
    }
    #endregion

    #region " Events "

    protected void btnSaveBasicInfo_Click(object sender, EventArgs e)
    {
        ProfileBasicMaster profileBasicMaster = new ProfileBasicMaster();
        DBAccess dBAccess = new DBAccess();
        profileBasicMaster.LoginId = Convert.ToInt32(Session[Constants.UserSession]);
        profileBasicMaster.ProfileId = dBAccess.GetProfileBasicID(Convert.ToInt32(Session[Constants.UserSession]));
        profileBasicMaster.ProfileCreateBy = ddlProfile.SelectedValue;
        profileBasicMaster.Gender = rblGender.SelectedValue == "0" ?true:false;
        profileBasicMaster.maritialStatus = Convert.ToInt32(rblMaritialStatus.SelectedValue);
        profileBasicMaster.Children = Convert.ToInt32(rblHaveChilden.SelectedValue);
        profileBasicMaster.MotherToungeLanguageId = Convert.ToInt32(ddlMotherTongue.SelectedValue);
        // datepicker
        profileBasicMaster.DateOfBirth = DateTime.Now;
        profileBasicMaster.PartnerPreference = txtPartnerPreference.Text;
        profileBasicMaster.AboutMe = txtAboutMe.Text;

        dBAccess.UpdateProfileBasicMaster(profileBasicMaster);

    }
    protected void btnEditBasicInfo_Click(object sender, EventArgs e)
    {

    }
    protected void btnCancelBasicInfo_Click(object sender, EventArgs e)
    {
        clearBasicInfo();
    }

    protected void btnReligiousInfoSave_Click(object sender, EventArgs e)
    {
        ReligiousInfoMaster religiousInfoMaster = new ReligiousInfoMaster();
        DBAccess dBAccess = new DBAccess();

        religiousInfoMaster.ReligiousInfoId = dBAccess.GetReligiosInfoID(Convert.ToInt32(Session[Constants.UserSession]));
        religiousInfoMaster.ReligionId = Convert.ToInt32(ddlReligion.SelectedValue);
        religiousInfoMaster.CastId = Convert.ToInt32(ddlCast.SelectedValue);
        religiousInfoMaster.SubCastId = Convert.ToInt32(ddlSubcast.SelectedValue);
        religiousInfoMaster.Gana = txtGana.Text;
        religiousInfoMaster.Gotra = txtGotra.Text;
        religiousInfoMaster.Nakshtra = txtNakshtra.Text;
        religiousInfoMaster.BirthTime = DateTime.Now; //Convert.ToDateTime(txtBirthTime.Text);
        religiousInfoMaster.IsManglik = Convert.ToInt32(rblMangalik.SelectedValue);
        religiousInfoMaster.SunSignId = Convert.ToInt32(ddlSunSign.SelectedValue);
        religiousInfoMaster.LoginId = Convert.ToInt32(Session[Constants.UserSession]);

        dBAccess.UpdateReligiousInfoMaster(religiousInfoMaster);
    }
    protected void btnReligiousInfoEdit_Click(object sender, EventArgs e)
    {

    }
    protected void btnReligiousInfoCancel_Click(object sender, EventArgs e)
    {
        clearReligiousInfo();
    }

    protected void btnEducationSave_Click(object sender, EventArgs e)
    {
        CareerInfoMaster careerInfoMaster = new CareerInfoMaster();
        DBAccess dBAccess = new DBAccess();

        careerInfoMaster.LoginId = Convert.ToInt32(Session[Constants.UserSession]);
        careerInfoMaster.CareerInfoId = dBAccess.GetCareerInfoID(Convert.ToInt32(Session[Constants.UserSession]));
        careerInfoMaster.EducationId = Convert.ToInt32(ddlEducation.SelectedValue);
        careerInfoMaster.OccupationId = Convert.ToInt32(ddlOccupation.SelectedValue);
        careerInfoMaster.RecidancyStatus = Convert.ToInt32(ddlResidentialType.SelectedValue);
        careerInfoMaster.AnnualIncome = Convert.ToDouble(txtAnnualIncome.Text);

        dBAccess.UpdateCareerInfoMaster(careerInfoMaster);
    }
    protected void btnEducationEdit_Click(object sender, EventArgs e)
    {

    }
    protected void btnEducationCancel_Click(object sender, EventArgs e)
    {
        clearCarrierInfo();
    }

    protected void btnPhysicalInfoSave_Click(object sender, EventArgs e)
    {
        PhysicalInfoMaster physicalInfoMaster = new PhysicalInfoMaster();
        DBAccess dBAccess = new DBAccess();

        physicalInfoMaster.PhysicalInfoId = dBAccess.GetPhysicalInfoID(Convert.ToInt32(Session[Constants.UserSession]));
        physicalInfoMaster.LoginId = Convert.ToInt32(Session[Constants.UserSession]);
        physicalInfoMaster.Height = ddlHeight.SelectedValue;
        physicalInfoMaster.Weight = ddlWeight.SelectedValue;
        physicalInfoMaster.BloodGroup = Convert.ToInt32(rblBloodGroup.SelectedValue);
        physicalInfoMaster.BodyType = Convert.ToInt32(rblBodyType.SelectedValue);
        physicalInfoMaster.BodyType = Convert.ToInt32(rblComplexion.SelectedValue);
        physicalInfoMaster.DietType = Convert.ToInt32(rblDietType.SelectedValue);
        physicalInfoMaster.PhysicallyChallengedStatus = chkPhysicallyChallenged.Checked;
        physicalInfoMaster.IsSmoke = chkIsSmoke.Checked;
        physicalInfoMaster.IsDrink = chkIsDrink.Checked;
        physicalInfoMaster.IsSpects = chkIsSpects.Checked;

        dBAccess.UpdatePhysicalInfoMaster(physicalInfoMaster);

    }
    protected void btnPhysicalInfoEdit_Click(object sender, EventArgs e)
    {

    }
    protected void btnPhysicalInfoCancel_Click(object sender, EventArgs e)
    {
        clearPhysicalInfo();
    }
    protected void btnFamilyBGSave_Click(object sender, EventArgs e)
    {
        FamilyBackGroundMaster familyBackGroundMaster = new FamilyBackGroundMaster();
        DBAccess dBAccess = new DBAccess();

        familyBackGroundMaster.FamilyBackGroundId = dBAccess.GetFamityBackgroudID(Convert.ToInt32(Session[Constants.UserSession]));
        familyBackGroundMaster.LoginId = Convert.ToInt32(Session[Constants.UserSession]);
        familyBackGroundMaster.FatherName = txtFatherName.Text;
        familyBackGroundMaster.FatherOccupationId = Convert.ToInt32(ddlFatherOccupatioin.SelectedValue);
        familyBackGroundMaster.MotherOccupationId = Convert.ToInt32(ddlMotherOccupation.SelectedValue);
        familyBackGroundMaster.MotherName = txtMotherName.Text;
        familyBackGroundMaster.NumberOfBrothers = Convert.ToInt32(txtNumberofBrothers.Text);
        familyBackGroundMaster.NumberOfSisters = Convert.ToInt32(txtNumberOfSisters.Text);
        familyBackGroundMaster.NumberOfBrothersMarried = Convert.ToInt32(txtMarriedBros.Text);
        familyBackGroundMaster.NumberOfSistersMarried = Convert.ToInt32(txtMarriedSisters.Text);
        familyBackGroundMaster.KnownRelativesWithAddress = txtKnownRelatives.Text;
        familyBackGroundMaster.AboutMyFamily = txtAboutMyfamily.Text;
        familyBackGroundMaster.IsJointFamily = chkIsJointFamily.Checked;
        familyBackGroundMaster.FamilyValues =  Convert.ToInt32(rblFamilyValues.SelectedValue);

        dBAccess.UpdateFamilyBackgroundMaster(familyBackGroundMaster);
    }
    protected void btnFamilyBGEdit_Click(object sender, EventArgs e)
    {

    }
    protected void btnFamilyBGCancel_Click(object sender, EventArgs e)
    {
        clearFamilyBackgroud();
    }
    #endregion

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (uploadImage.HasFile)
        {
            //profile_image.ImageUrl = "~/Image/" + onlyname;

            string serverDirPath = Server.MapPath("ProfileImages") + "\\";
            string webDirPath = ConfigurationManager.AppSettings["SitePath"].ToString();
            Guid imgId = Guid.NewGuid();
            string WebImgPath = webDirPath+ "ProfileImages/" + imgId.ToString() + ".jpg";
            string ServerImgPath = serverDirPath + "\\" + imgId.ToString() + ".jpg";
            //Response.ContentType = "image/JPEG";
            System.Drawing.Image img = Bitmap.FromStream(uploadImage.PostedFile.InputStream);
            img.Save(ServerImgPath, ImageFormat.Jpeg);

            DBAccess dBAccess = new DBAccess();
            dBAccess.UpdateProfilePhoto(0, Convert.ToInt32(Session[Constants.UserSession]), uploadImage.PostedFile.FileName, WebImgPath, false);
        }
    }
}
